package com.modulemanager.views

import android.graphics.Color
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.FrameLayout
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.constraintlayout.widget.ConstraintSet
import androidx.fragment.app.Fragment
import com.libbase.Weak
import com.libmodulemanager.R
import com.libservice.ServiceManager
import com.libui.navigatonbar.NavigationBar
import com.modulemanager.models.NavigationBarStyle
import com.modulemanager.page.PageInterface
import io.reactivex.rxjava3.disposables.CompositeDisposable
import io.reactivex.rxjava3.disposables.Disposable
import io.reactivex.rxjava3.kotlin.addTo

abstract class PageFragment(page: PageInterface?) : Fragment(), PageFragmentContainer {
    companion object {
        const val TAG = "PageFragment"
    }

    final override var page: PageInterface? by Weak { page }

    private var compositeDisposable = CompositeDisposable()

    override val fragment: Fragment
        get() {
            return this
        }

    private lateinit var navigationBar: NavigationBar

    lateinit var content: FrameLayout

    private var pageTitleDisposable: Disposable? = null

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?
    ): View? {
        return inflater.inflate(R.layout.fragment_page, container, false)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        content = view.findViewById(R.id.content_container)

        navigationBar = view.findViewById(R.id.navigation_bar)

        navigationBar.onMenuTap = { menuData ->
            menuData?.url?.let { url ->
                ServiceManager.instance.routerService.open(url, null, null)
            } ?: run {
                menuData?.onTap?.let { onTap ->
                    menuData.webView?.let { webViewEx ->
                        webViewEx.evaluateJavascriptInMainThread(onTap) {
                            Log.i(TAG, "${menuData.title} onTap, result is $it")
                        }
                    }
                }
            }
        }

        page?.config?.navigationBarStyle
            ?.subscribe { _: NavigationBarStyle ->
                initNavigationBar()
            }
            ?.addTo(compositeDisposable)

        page?.config?.rightMenus
            ?.subscribe {
                navigationBar.rightMenus = it.value
            }
            ?.addTo(compositeDisposable)

        page?.config?.statusBarType
            ?.subscribe { _ ->
                page?.updateStatusBar()
            }
            ?.addTo(compositeDisposable)
    }

    override fun onDestroy() {
        super.onDestroy()

        pageTitleDisposable?.dispose()

        page?.let {
            it.module?.removePage(it.type, it.id)
        }

        compositeDisposable.dispose()
    }

    override fun onResume() {
        super.onResume()

        page?.updateStatusBar()
    }

    private fun initNavigationBar() {
        val pageConfig = page?.config ?: return
        val navigationBarStyle = pageConfig.navigationBarStyle.value ?: return

        when (navigationBarStyle.type) {
            NavigationBarStyle.NavigationBarType.HIDE -> {
                navigationBar.visibility = View.GONE
                return
            }

            NavigationBarStyle.NavigationBarType.OPAQUE -> {

            }

            NavigationBarStyle.NavigationBarType.TRANSPARENT -> {
                navigationBar.setBackgroundColor(Color.TRANSPARENT)
                navigationBar.visibility = View.VISIBLE
                val parentView = content.parent as ConstraintLayout
                val cs = ConstraintSet()
                cs.clone(parentView)
                val pId = ConstraintLayout.LayoutParams.PARENT_ID
                cs.connect(content.id, ConstraintSet.TOP, pId, ConstraintSet.TOP)
                cs.applyTo(parentView)
            }
        }

        pageConfig.navigationBarStyle.value?.backgroundColor?.let {
            navigationBar.setBackgroundColor(it)
        }

        pageConfig.navigationBarStyle.value?.tintColor?.let {
            navigationBar.titleView.setTextColor(it)
            navigationBar.backButton.setTextColor(it)
        }

        navigationBar.visibility = View.VISIBLE

        navigationBar.setTitle(pageConfig.title, pageConfig.subTitle)
        navigationBar.titleGravity = pageConfig.titleAlignment.gravity

        navigationBar.rightMenus = pageConfig.rightMenus.value?.value

        pageTitleDisposable?.dispose()
        pageTitleDisposable = pageConfig.titleObservable.subscribe { titlePair ->
            navigationBar.setTitle(titlePair.first, titlePair.second)
        }
    }
}